using Assets.Scripts.GDK;
using System.Collections.Generic;
using UnityEngine;

namespace Brick
{
	public class EnemyPool
	{
		private static EnemyPool instance;

		private Dictionary<string, List<GameObject>> pool;

		private Dictionary<string, GameObject> prefabs;

		private EnemyPool()
		{
			pool = new Dictionary<string, List<GameObject>>();
			prefabs = new Dictionary<string, GameObject>();
		}

		public static EnemyPool GetInstance()
		{
			if (instance == null)
			{
				instance = new EnemyPool();
			}
			return instance;
		}

		public int GetObjCount(string objName)
		{
			if (pool.ContainsKey(objName))
			{
				return pool[objName].Count;
			}
			return 0;
		}

		public void CreateObj(int num, string objName, string pathName, Transform poolParent)
		{
			GameObject gameObject = null;
			if (prefabs.ContainsKey(objName))
			{
				gameObject = prefabs[objName];
			}
			else
			{
				gameObject = (GameObject)Resources.Load(pathName);
				prefabs.Add(objName, gameObject);
			}
			for (int i = 0; i < num; i++)
			{
				GameObject gameObject2 = Object.Instantiate(gameObject);
				gameObject2.transform.parent = poolParent;
				gameObject2.name = objName;
				gameObject2.SetActive(value: false);
				if (pool.ContainsKey(objName))
				{
					pool[objName].Add(gameObject2);
				}
				else
				{
					pool.Add(gameObject2.name, new List<GameObject>
				{
					gameObject2
				});
				}
			}
		}

		public GameObject GetObj(string objName, string pathName)
		{
			GameObject gameObject = null;
			if (pool.ContainsKey(objName) && pool[objName].Count > 0)
			{
				gameObject = pool[objName][0];
				if ((bool)gameObject)
				{
					gameObject.SetActive(value: true);
					pool[objName].Remove(gameObject);
					return gameObject;
				}
				gdk_game_manager.log("-----------========== find nul");
			}
			GameObject gameObject2 = null;
			if (prefabs.ContainsKey(objName))
			{
				gameObject2 = prefabs[objName];
			}
			else
			{
				gameObject2 = (GameObject)Resources.Load(pathName);
				prefabs.Add(objName, gameObject2);
			}
			gameObject = Object.Instantiate(gameObject2);
			gameObject.name = objName;
			return gameObject;
		}

		public void RecycleObj(GameObject obj)
		{
			obj.SetActive(value: false);
			obj.transform.parent = PoolPlayer.Instance.GetObj();
			if (pool.ContainsKey(obj.name))
			{
				pool[obj.name].Add(obj);
			}
			else
			{
				pool.Add(obj.name, new List<GameObject>
			{
				obj
			});
			}
		}
	}
}

